<?php

/**
 * This is the model class for table "{{cmp_deposit}}".
 *
 * The followings are the available columns in table '{{cmp_deposit}}':
 * @property integer $zcd_id
 * @property integer $zm_id
 * @property integer $zcd_type
 * @property string $zcd_event
 * @property string $zcd_orderid
 * @property double $zcd_amount
 * @property double $zcd_amount_log
 * @property string $zcd_note
 * @property integer $inputtime
 */
class CmpDeposit extends CActiveRecord
{
	//成交时间，用于搜索
	public $limit_time;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CmpDeposit the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cmp_deposit}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zcd_event', 'required'),
			array('zm_id, zcd_type, inputtime', 'numerical', 'integerOnly'=>true),
			array('zcd_amount, zcd_amount_log', 'numerical'),
			array('zcd_event', 'length', 'max'=>100),
			array('zcd_orderid', 'length', 'max'=>30),
			array('zcd_note', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zcd_id, zm_id, zcd_type, zcd_event, zcd_orderid, zcd_amount, zcd_amount_log, zcd_note, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				 'company'=>array(self::BELONGS_TO, 'company', 'zm_id'),//商家
				// ''
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zcd_id' => 'Zcd',
			'zm_id' => 'Zm',
			'zcd_type' => 'Zcd Type',
			'zcd_event' => 'Zcd Event',
			'zcd_orderid' => 'Zcd Orderid',
			'zcd_amount' => '金额',
			'zcd_amount_log' => 'Zcd Amount Log',
			'zcd_note' => 'Zcd Note',
			'inputtime' => 'Inputtime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zcd_id',$this->zcd_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zcd_type',$this->zcd_type);
		$criteria->compare('zcd_event',$this->zcd_event,true);
		$criteria->compare('zcd_orderid',$this->zcd_orderid,true);
		$criteria->compare('zcd_amount',$this->zcd_amount);
		$criteria->compare('zcd_amount_log',$this->zcd_amount_log);
		$criteria->compare('zcd_note',$this->zcd_note,true);

		
		if(!empty($this->limit_time)){
			switch($this->limit_time){
				case 1:
					$criteria->addCondition('t.inputtime >'.time()-3600*24*30);//近一个月
					break;
				case 2:
					$criteria->addCondition('t.inputtime >'.time()-3600*24*30*3);//近三个月
					break;
				case 3:
					$criteria->addCondition('t.inputtime <'.time()-3600*24*30*3);//三个月前
					break;
				default:
					break;
			}
		}
		
		$criteria->order = "inputtime DESC ,zcd_id DESC";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>12,		
			),
		));
	}
	
	/**
	 * 服务类型: 
	 */
	public function getZcdType($type=null){
		if($type==null){
			return array(
				''=>'请选择',
				'1'=>'商家补充保证金',
				'2'=>'买家退款'
			);
		}else{
			$level = $this->getZcdType();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 操作类型:
	 */
	public function getActType($type=null){
		if($type==null){
			return array(
					''=>'请选择',
					'1'=>'充值',
					'2'=>'扣除',
					'3'=>'套现'
			);
		}else{
			$level = $this->getActType();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 *成交时间 
	 */
	public function getLimitTime($type=null){
		if($type==null){
			return array(
					''=>'请选择',
					'1'=>'最近一个月',
					'2'=>'最近三个月',
					'3'=>'三个月之前',
			);
		}else{
			$level = $this->getLimitTime();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->inputtime = time();
		}
		return true;
	}
}